package com.zrrd.yunchmall.user.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.yunchmall.user.entity.*;
import com.zrrd.yunchmall.user.mapper.*;
import com.zrrd.yunchmall.user.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@Service
@SuppressWarnings("all")
//你想返回缓存结果的方法前都可以加上此注解, 加上此注解的方法的返回结果都会缓存在redis
//Role:ListMemu_5
@Cacheable(value = "Role", key = "#root+'_'+#roleId")
//@Cacheable(value = "Role", key = "#root.args[0]")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleResourceRelationMapper roleResourceRelationMapper;
    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<Menu> getmenulist(long roleId) {
        return menuMapper.selectMenusByRoleId(roleId);
    }

    @Override
    public List<Resource> getresourcelist(long roleId) {
        return resourceMapper.selectResourceByRoleId(roleId);
    }


    @Override
    @Transactional//将这个方法至于事务中，事务具有原子性（要不都做，要不都不做，如果中间出错就回滚事务），基于环绕通知与异常通知实现
    @Cacheable(value = "Role", key = "#root.args[0]", cacheManager = "cacheManager1Minute")
    public void allocRole(long roleId, String menuIds) {
        //1.移除原来的角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", roleId);
        // delete from ums_admin_role_relation where admin_id = ?
        //基于动态代理的模式实现的
        roleMenuRelationMapper.delete(queryWrapper);
        //2.给他分配当前全部的角色
        if(!StringUtil.isEmpty(menuIds)) {
            for (String menuId:menuIds.split(",")) {
                //封装一个relation对象
                RoleMenuRelation relation = new RoleMenuRelation();
                relation.setRoleId(roleId);
                relation.setMenuId(Long.valueOf(menuId));
                //添加这个relation对象
                roleMenuRelationMapper.insert(relation);
            }
        }
        //3.将1和2至于一个事务当中
    }

    @Override
    @Transactional
    @Cacheable(value = "Role", key = "#root.args[0]", cacheManager = "cacheManager5Minute")//将这个方法至于事务中，事务具有原子性（要不都做，要不都不做，如果中间出错就回滚事务），基于环绕通知与异常通知实现
    public void allocResouce(long roleId, String resourceIds) {
        //1.移除原来的角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", roleId);
        // delete from ums_admin_role_relation where admin_id = ?
        //基于动态代理的模式实现的
        roleResourceRelationMapper.delete(queryWrapper);
        //2.给他分配当前全部的角色
        if(!StringUtil.isEmpty(resourceIds)) {
            for (String resourceId:resourceIds.split(",")) {
                //封装一个relation对象
                RoleResourceRelation relation = new RoleResourceRelation();
                relation.setRoleId(roleId);
                relation.setResourceId(Long.valueOf(resourceId));
                //添加这个relation对象
                roleResourceRelationMapper.insert(relation);
            }
        }
        //3.将1和2至于一个事务当中
    }
}

